<?php
use OSS\OssClient;
use OSS\Core\OssException;
use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
    /**
     * 昵称生成规则
     *
     * @author : chenhan
     * @access public|private|protected
     *
     * @param  mixed $phone 手机号
     *
     * @return string
     */
    /* function nickname($phone)
    {
        if (empty($phone)) {
            $phone = rand(1111, 9999);
        }
        $str = '';
        //$str = 'TLW'.substr($phone,-3,3).implode(array_reverse(str_split(substr($phone,3,3))));
        //$str = 'TLW'.rand(11,99).substr($phone,-4,4);
        $str = 'TLW_' . date('Ymd', time()) . rand(1111, 9999);

        return $str;
    } */

    /**
     * 获取客户端IP地址
     *
     * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
     * @param boolean $adv  是否进行高级模式获取（有可能被伪装）
     *
     * @return mixed
     */
    /* function get_client_ip($type = 0, $adv = false)
    {
        $type = $type ? 1 : 0;
        static $ip = NULL;
        if ($ip !== NULL) return $ip[ $type ];
        if ($adv) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                $pos = array_search('unknown', $arr);
                if (false !== $pos) unset($arr[ $pos ]);
                $ip = trim($arr[0]);
            } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (isset($_SERVER['REMOTE_ADDR'])) {
                $ip = $_SERVER['REMOTE_ADDR'];
            }
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = sprintf("%u", ip2long($ip));
        $ip = $long ? [$ip, $long] : ['0.0.0.0', 0];
        return $ip[ $type ];
    } */
    /*oppo回调处理*/
    /* function get_param($param_name)   //兼容post/get
    {
        $param_value = "";
        if(isset($_POST[$param_name])){
            $param_value = trim($_POST[$param_name]);
        }else if(isset($_GET[$param_name])){
            $param_value = trim($_GET[$param_name]);
        }
        return $param_value;
    }
    function rsa_verify($contents,$publickey) {
        $str_contents = "notifyId={$contents['notifyId']}&partnerOrder={$contents['partnerOrder']}&productName={$contents['productName']}&productDesc={$contents['productDesc']}&price={$contents['price']}&count={$contents['count']}&attach={$contents['attach']}";
        //$publickey= $this->config['OPPO']['PUBLIC_KEY'];
        $pem = chunk_split($publickey,64,"\n");
        $pem = "-----BEGIN PUBLIC KEY-----\n".$pem."-----END PUBLIC KEY-----\n";
        $public_key_id = openssl_pkey_get_public($pem);
        $signature =base64_decode($contents['sign']);
        return openssl_verify($str_contents, $signature, $public_key_id );   //成功返回1,0失败，-1错误,其他看手册
    } */
    /*华为生成签名*/
    /* function rsa_sign($data,$priKey) {
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($priKey, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";
        openssl_sign ( $data, $sign, $res , OPENSSL_ALGO_SHA256);
        $sign = base64_encode ( $sign );
        return $sign;
    } */
    /*华为验签*/
    // function hw_verify($data, $sign,$pubKey){
    //     /*测试*/
    //     //$pubKey= "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIW1g+KAqqOeC1ypte8L3qTDk2nz6jUbM6o6Jg9obvivPnCAm/wZvV3jWbYWfOuO/wrFJygn/jZqf8cR1T1CQa8CAwEAAQ==";
    //     $res = "-----BEGIN PUBLIC KEY-----\n" .
    //         wordwrap($pubKey, 64, "\n", true) .
    //         "\n-----END PUBLIC KEY-----";
    //     $result = (openssl_verify($data, base64_decode($sign), $res, OPENSSL_ALGO_SHA256)===1);
    //     return $result;
    // }
    /**
     * 短信发送
     * @param $to    接收人
     * @param $model 短信模板ID
     * @param $code   短信验证码
     * @return json
     * @
     */
    /* function send_sms($to, $model, $code)
    {
        require_once '../extend/alisms/vendor/autoload.php';
        Config::load(); //加载区域结点配置
        $config = '根据你的实际情况读取配置文件或读取数据库，本项目是将配置写入数据库实现';
        $accessKeyId = 'LTAIuFOC03oi9x2t';
        $accessKeySecret = 'JoCwWrCm1OsH1HJ10xOOWJMHikNdrl';
        $templateParam = $code;
        //短信签名
        $signName = '河马游戏';
        //短信模板ID
        switch ($model) {
            case 1:
                $templateCode = 'SMS_168410170';  // 注册登录短信验证码模板
                break;
            case 2:
                $templateCode = $config['model_code_reset'];  // 重置密码短信验证码模板
                break;
        }
        //短信API产品名（短信产品名固定，无需修改）
        $product = "Dysmsapi";
        //短信API产品域名（接口地址固定，无需修改）
        $domain = "dysmsapi.aliyuncs.com";
        //暂时不支持多Region（目前仅支持cn-hangzhou请勿修改）
        $region = "cn-hangzhou";
        // 初始化用户Profile实例
        $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
        // 增加服务结点
        DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
        // 初始化AcsClient用于发起请求
        $acsClient = new DefaultAcsClient($profile);
        // 初始化SendSmsRequest实例用于设置发送短信的参数
        $request = new SendSmsRequest();
        // 必填，设置雉短信接收号码
        $request->setPhoneNumbers($to);
        // 必填，设置签名名称
        $request->setSignName($signName);
        // 必填，设置模板CODE
        $request->setTemplateCode($templateCode);
        // 可选，设置模板参数
        if ($templateParam) {
            $request->setTemplateParam(json_encode($templateParam));
        }
        //发起访问请求
        $acsResponse = $acsClient->getAcsResponse($request);
        //返回请求结果
        $result = json_decode(json_encode($acsResponse), true);
        // 具体返回值参考文档：https://help.aliyun.com/document_detail/55451.html?spm=a2c4g.11186623.6.563.YSe8FK
        return $result;
    } */




